import storage from "@/utils/storage";
import { login, getInfo, managelogin } from "@/api/login";
import useTabbarStore from "./tabbar";
import useRouteStore from "./route";
import useMenuStore from "./menu";
import { TOKEN, ACCOUNT } from "../mutation-types";

const useUserStore = defineStore(
  // 唯一ID
  "user",
  {
    state: () => ({
      account: storage.local.get(ACCOUNT) || "",
      token: storage.local.get(TOKEN) || "",
      permissions: [],
      name: "",
      userType: "",
      welcome: "",
      avatar: "",
      roles: [],
      portalConfigs: [],
      defaultPortal: {},
      info: {},
      platformVersion: "",
      sysNoticeList: [],
      company: {},
      dept: {}
    }),
    mutations: {
      SET_TOKEN: (state, token) => {
        state.token = token;
      },
      SET_ACCOUNT: (state, account) => {
        state.account = account;
      },
      SET_COMPANY: (state, company) => {
        state.company = company;
      },
      SET_DEPT: (state, dept) => {
        state.dept = dept;
      },
      SET_NAME: (state, name) => {
        state.name = name;
      },
      SET_AVATAR: (state, avatar) => {
        state.avatar = avatar;
      },
      SET_ROLES: (state, roles) => {
        state.roles = roles;
      },
      SET_INFO: (state, info) => {
        state.info = info;
      },
      SET_PERMISSIONS: (state, permissions) => {
        state.permissions = permissions;
      },
      SET_USER_TYPE: (state, userType) => {
        state.userType = userType;
      },
      SET_PORTAL_CONFIG: (state, portalConfigs) => {
        state.portalConfigs = portalConfigs;
      },
      SET_DEFAULT_PORTAL: (state, defaultPortal) => {
        state.defaultPortal = defaultPortal;
      },
      SET_PLATFORM_VERSION: (state, platformVersion) => {
        state.platformVersion = platformVersion;
      },
      SET_NOTICE_LIST: (state, sysNoticeList) => {
        state.sysNoticeList = sysNoticeList;
      }
    },
    getters: {
      isLogin: (state) => {
        let retn = false;
        if (state.token) {
          // if (new Date().getTime() < state.failure_time * 1000) {
          retn = true;
          // }
        }
        return retn;
      }
    },
    actions: {
      setToken(token) {
        this.token = token;
      },
      login(data) {
        return new Promise((resolve, reject) => {
          // 通过 api 进行登录
          login(data)
            .then((res) => {
              storage.local.set(TOKEN, res.token, 7 * 24 * 60 * 60 * 1000);
              storage.local.set(
                ACCOUNT,
                data.username,
                7 * 24 * 60 * 60 * 1000
              );
              this.token = res.token;
              this.account = data.username;
              resolve(res);
            })
            .catch((error) => {
              reject(error);
            });
        });
      },
      tokenlogin(data) {
        return new Promise((resolve, reject) => {
          // 通过 api 进行登录
          console.log("params", data);
          managelogin(data)
            .then((res) => {
              storage.local.set(TOKEN, res.token, 7 * 24 * 60 * 60 * 1000);
              this.token = res.token;
              resolve(res);
            })
            .catch((error) => {
              reject(error);
            });
        });
      },
      logout() {
        return new Promise((resolve) => {
          const tabbarStore = useTabbarStore();
          const routeStore = useRouteStore();
          const menuStore = useMenuStore();
          storage.local.remove(TOKEN);
          this.token = "";
          this.permissions = [];
          tabbarStore.clean();
          routeStore.removeRoutes();
          menuStore.setActived(0);
          resolve();
        });
      },
      // 获取我的信息
      getInfo() {
        return new Promise((resolve) => {
          // 通过 mock 获取权限
          getInfo().then((res) => {
            // storage.local.set("permissions", res.permissions);
            this.permissions = res.permissions;
            storage.local.set(
              ACCOUNT,
              "(" + res.user.company.companyName + ")" + res.user.userName,
              7 * 24 * 60 * 60 * 1000
            );
            this.account =
              "(" + res.user.company.companyName + ")" + res.user.userName;
            console.log(this.permissions);
            resolve(res);
          });
        });
      },

      // 获取我的权限
      getPermissions() {
        return new Promise((resolve) => {
          resolve(this.permissions);
        });
      }
    }
  }
);

export default useUserStore;
